﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class Flash : MonoBehaviour 
{
	
	public Vector3 startPos;
	public Vector3 endPos;//确定位置

	private LineRenderer lineRenderer;

	public int pointNumber;//设置波动点个数
	public void Refresh()
    {
		Vector3 d = (endPos - startPos) / (pointNumber + 2);//两点之间均分等分

		for (int i = 1; i < pointNumber + 1; i++)
		{
			float height = (Random.value - 0.5f) * offset;

			float x = (Random.value * 0.5f - 0.5f) * 0.5f;

			lineRenderer.SetPosition(i, transform.position + startPos + d * i + new Vector3(x, height, 0));
		}//循环随即设置波动点位置
		lineRenderer.SetPosition(0, startPos + transform.position);
		lineRenderer.SetPosition(lineRenderer.positionCount - 1, endPos + transform.position);
		timer = 0.0f;
	}
    private void OnEnable()
    {
        
		lineRenderer = GetComponent<LineRenderer>();

		lineRenderer.positionCount = pointNumber +2;//波动点加上起点和终点

	}
	
	private float timer = 0.0f;
	public float offset;//波动范围
	public float delay;//频率
	void FixedUpdate () 
	{
		timer+=Time.deltaTime;
		if(timer>delay)
		{
			Vector3 d =  (endPos - startPos)/(pointNumber+2);//设置波动点
		
			for(int i =1; i< pointNumber+1; i++)
			{
				float height = (Random.value-0.5f)*offset;

				float x = (Random.value*0.5f-0.5f)*0.5f;

				lineRenderer.SetPosition(i,transform.position+ startPos+ d*i+ new Vector3(x,height,0)); 
			}//循环随即设置波动点位置
			lineRenderer.SetPosition(0,startPos+transform.position);
			lineRenderer.SetPosition(lineRenderer.positionCount-1,endPos+transform.position);
			timer =0.0f;
		}
	}
}
